import { createParamDecorator, ExecutionContext } from '@nestjs/common';

/**
 * 验证器消息装饰器
 * 基于PHP的验证器分组方式，符合NestJS规范
 * 
 * 使用方式:
 * @ValidateMessage('validate_user', 'username_require')
 * username: string;
 */
export const ValidateMessage = (group: string, key: string) => {
  return (target: any, propertyKey: string) => {
    // 存储验证器分组和键值信息
    if (!target.constructor._validateMessages) {
      target.constructor._validateMessages = new Map();
    }
    target.constructor._validateMessages.set(propertyKey, { group, key });
  };
};

/**
 * 场景化验证装饰器
 * 基于PHP的$scene数组方式，符合NestJS规范
 * 
 * 使用方式:
 * @ValidateScene('add', ['username', 'password'])
 * @ValidateScene('edit', ['username'])
 */
export const ValidateScene = (scene: string, fields: string[]) => {
  return (target: any) => {
    if (!target._validateScenes) {
      target._validateScenes = new Map();
    }
    target._validateScenes.set(scene, fields);
  };
};

/**
 * 获取验证器消息键值
 * 用于在验证管道中获取正确的消息键值
 */
export const getValidateMessageKey = (target: any, propertyKey: string): { group: string; key: string } | null => {
  if (target.constructor._validateMessages) {
    return target.constructor._validateMessages.get(propertyKey) || null;
  }
  return null;
};

/**
 * 获取验证器场景字段
 * 用于场景化验证
 */
export const getValidateSceneFields = (target: any, scene: string): string[] => {
  if (target._validateScenes) {
    return target._validateScenes.get(scene) || [];
  }
  return [];
};
